//
//  VideoListViewController.m
//  Tongcheng
//
//  Created by 邢广范 on 2017/4/8.
//  Copyright © 2017年 Tongcheng. All rights reserved.
//

#import "VideoListViewController.h"
#import "VideoListCollectionViewCell.h"
#import "VideoListOperator.h"
#import "VideoModel.h"
#import "VideoDataModel.h"
#import "VideoListDataModel.h"
#import "VideoStartViewController.h"
#import "MJRefresh.h"

@interface VideoListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong) MJRefreshAutoNormalFooter *footer;

@end

@implementation VideoListViewController

static NSString *videoListCellIdentifier = @"VideoListCollectionViewCell";

- (MJRefreshAutoNormalFooter *)footer
{
    if (_footer == nil) {
        _footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(downloadMore)];
        
    }
    return _footer;
}

- (void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    [self.collectionView reloadData];
}

- (NSInteger)page
{
    if (!_page) {
        _page = 1;
    }
    return _page;
}

- (void)setupNavigation
{
    self.navigationItem.title = @"视频秀";
}

- (void)setupView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((kScreenWidth - 15) / 2, (kScreenWidth - 15) * 10 / 17);
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.minimumLineSpacing = 5;
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 5, kScreenWidth, kScreenHeight - 69) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = YES;
    self.collectionView.backgroundColor = [UIColor clearColor];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"VideoListCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:videoListCellIdentifier];
    [self.view addSubview: self.collectionView];
    
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self download];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigation];
    [self setupView];
    [self download];
    // Do any additional setup after loading the view from its nib.
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    VideoListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:videoListCellIdentifier forIndexPath:indexPath];
    VideoListDataModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    VideoStartViewController *startVc = [[VideoStartViewController alloc] init];
    VideoListDataModel *model = self.dataArray[indexPath.row];
    startVc.videoId = model._id;
    startVc.isShowBottomView = NO;
    [self.navigationController pushViewController:startVc animated:YES];
}

- (void)download
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    dic[@"Page"] = [NSString stringWithFormat:@"%ld", self.page];
    dic[@"Row"] = @"30";
    dic[@"uid "] = self.userId;
    VideoListOperator *operator = [[VideoListOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        [self.collectionView.mj_header endRefreshing];
        if ([baseModel.code isEqualToString:@"1"]) {
            self.page ++;
            self.dataArray = [NSMutableArray arrayWithArray:operator.data.data.list];
            if (self.dataArray.count < 30) {
                self.collectionView.mj_footer = nil;
            }else{
                self.collectionView.mj_footer = self.footer;
            }

        }else{
            
        }
    } failBlock:^(BaseModel *baseModel) {
        [self.collectionView.mj_header endRefreshing];
    }];
}

- (void)downloadMore
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    dic[@"Page"] = [NSString stringWithFormat:@"%ld", self.page];
    dic[@"Row"] = @"30";
    dic[@"uid "] = self.userId;
    VideoListOperator *operator = [[VideoListOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        [self.collectionView.mj_footer endRefreshing];
        if ([baseModel.code isEqualToString:@"1"]) {
            self.page ++;
            NSArray *array = operator.data.data.list;
            if (array.count < 30) {
                self.collectionView.mj_footer = nil;
            }else{
                self.collectionView.mj_footer = self.footer;
            }
            NSMutableArray *MutableArray = [NSMutableArray arrayWithArray:self.dataArray];
            [MutableArray addObjectsFromArray:array];
            self.dataArray = MutableArray;
        }else{
            
        }
    } failBlock:^(BaseModel *baseModel) {
        [self.collectionView.mj_footer endRefreshing];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
